package com.yylending.las.main;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.statistics.StatService;
import com.yylending.las.Constants;
import com.yylending.las.Global;
import com.yylending.las.R;
import com.yylending.las.StatIDSet;
import com.yylending.las.lending.*;
import com.yylending.las.lending.increase.PreIncreaseLimitFragment;
import com.yylending.las.personal.PersonalCenterFragment;
import com.yylending.las.repay.repayrecord.NewRepaymentFragment;

import java.util.HashSet;

/**
 * Created by HenryZhang on 2017/7/7.<br>
 * Copyright © 2017 www.yylending.com. All Rights Reserved.<br><br>
 */
public class MainController {
	private final static String LOADING_FRAGMENT = "loading_fragment";

	/**
	 * 借款页
	 */
	private final static String NEED_APPLY_FRAGMENT = "need_apply_fragment";
	/**
	 * 还钱页
	 */
	private final static String REPAYMENT_FRAGMENT = "repayment_fragment";
	/**
	 * 我页
	 */
	private final static String PERSONAL_CENTER_FRAGMENT = "personal_center_fragment";

	/**
	 * 额度计算中
	 */
	private final static String CALCULATING_FRAGMENT = "calculating_fragment";
	/**
	 * 可提额
	 */
	private final static String INCREASE_FRAGMENT = "increase_fragment";
	/**
	 * 提额成功
	 */
	private final static String INCREASE_SUCCESS_FRAGMENT = "increase_success_fragment";
	/**
	 * 零额度
	 */
	private final static String ZERO_QUOTA_FRAGMENT = "zero_quota_fragment";
	/**
	 * 不需要现调的借款后页面
	 */
	private final static String NO_SITE_INVESTIGATION_LENDING_FRAGMENT = "site_investigation_lending_fragment";
	/**
	 * 需要现调的借款后页面
	 */
	private final static String SITE_INVESTIGATION_LENDING_FRAGMENT = "no_site_investigation_lending_fragment";
	/**
	 * 有在途借款
	 */
	private final static String HAVE_LOAN_FRAGMENT = "have_loan_fragment";
	/**
	 * 有额度(低额度，可提额；低额度，不可提额；低额度，提额中；高额度)
	 */
	private final static String HAVE_QUOTA_FRAGMENT = "have_quota_fragment";
	/**
	 * 强制提额 预约门店后界面
	 */
	private final static String LOW_QUOTA_OUTLET_APPOINTED_FRAGMENT = "low_quota_outlet_appointed_fragment";

	private static HashSet<String> mPageRecorder = new HashSet<>();

	private static boolean showContent(MainActivity aty, Fragment instance, String state) {
		boolean isFirstShow = mPageRecorder.contains(state);
		FragmentManager fm = aty.getSupportFragmentManager();
		FragmentTransaction ft = fm.beginTransaction();
		ft.setCustomAnimations(R.anim.fragment_fade_in, R.anim.fragment_fade_out);
		ft.replace(R.id.main_content_container, instance, state);
		ft.commitAllowingStateLoss();
		mPageRecorder.add(state);
		return isFirstShow;
	}

	static void switchState(MainActivity aty, int page) {
		Fragment target;
		switch (page) {
			case Constants.PAGE_LOADING://todo switch(string)可省掉Constants.PAGE_LOADING之类的定义
				target = LoadingFragment.newInstance(aty.mGetQuotaFail);
				boolean isFirstShow = showContent(aty, target, LOADING_FRAGMENT);
				if (!isFirstShow) {
					EventAgent.post(new QuotaEvent.ShowLoadingEvent(QuotaEvent.ShowLoadingEvent.QUOTA_SHOW_LOADING, aty.mGetQuotaFail));
				}
				break;
			case Constants.PAGE_NOT_APPLY:
				target = NotApplyFragment.newInstance(aty.mApplyStatus, aty.mLocation);
				showContent(aty, target, NEED_APPLY_FRAGMENT);
				break;
			case Constants.PAGE_CALCULATING:
				target = CalculatingFragment.newInstance();
				showContent(aty, target, CALCULATING_FRAGMENT);
				break;
			case Constants.PAGE_ZERO_QUOTA:
				target = ZeroQuotaFragment.newInstance(aty.mApplyStatus);
				showContent(aty, target, ZERO_QUOTA_FRAGMENT);
				break;
			case Constants.PAGE_INCREASE:
				target = PreIncreaseLimitFragment.newInstance(aty.mApplyStatus);
				showContent(aty, target, INCREASE_FRAGMENT);
				break;
			case Constants.PAGE_INCREASE_SUCCESS:
				StatService.onEvent(aty, StatIDSet.EVENT_ID_NO_ONLINE_QUOTA);
				target = IncreaseSuccessFragment.newInstance();
				showContent(aty, target, INCREASE_SUCCESS_FRAGMENT);
				break;
			case Constants.PAGE_HAVE_LOAN:
				target = HaveLoanFragment.newInstance();
				showContent(aty, target, HAVE_LOAN_FRAGMENT);
				break;
			case Constants.PAGE_COMPLEX_STATUS:
				target = ComplexStatusFragment.newInstance(aty.mApplyStatus, aty.mSavedTab);
				showContent(aty, target, HAVE_QUOTA_FRAGMENT);
				break;
			case Constants.PAGE_NO_LOAN_STATUS_RESEARCH:
				target = NoSiteInvestigationLendingFragment.newInstance(aty.mApplyStatus, aty.mVideoVerifyHintAuto);
				aty.mVideoVerifyHintAuto = false;
				showContent(aty, target, NO_SITE_INVESTIGATION_LENDING_FRAGMENT);
				break;
			case Constants.PAGE_LOAN_STATUS_RESEARCH:
				target = SiteInvestigationLendingFragment.newInstance(aty.mApplyStatus);
				showContent(aty, target, SITE_INVESTIGATION_LENDING_FRAGMENT);
				break;
			case Constants.PAGE_REPAYMENT:
				target = NewRepaymentFragment.newInstance();
				showContent(aty, target, REPAYMENT_FRAGMENT);
				break;
			case Constants.PAGE_PERSONAL_CENTER:
				target = PersonalCenterFragment.newInstance(Constants.PAGE_PERSONAL_CENTER);
				showContent(aty, target, PERSONAL_CENTER_FRAGMENT);
				break;
			case Constants.PAGE_LOW_QUOTA_OUTLET_APPOINTED:
				target = LowQuotaOutletSearchFragment.newInstance();
				showContent(aty, target, LOW_QUOTA_OUTLET_APPOINTED_FRAGMENT);
				break;
			default:
				break;
		}
	}

	public static void initRedDot(View view) {
		view.setVisibility(Global.sHasNewMsg ? View.VISIBLE : View.GONE);
	}

	public static void hideRedDot(View view) {
		view.setVisibility(View.GONE);
		Global.sHasNewMsg = false;
	}
}
